﻿using System;
using System.Collections;
using System.Collections.Generic;
using System.Text;
using OgilvyOne.UKNow.DALFactory;
using ECCIT;
using System.Linq;
using OgilvyOne.UKNow.IDAL;
using OgilvyOne.UKNow.ServiceReference;
using OgilvyOne.Model;
using OgilvyOne.Utility;
using OgilvyOne.BLL;
using Lucene.Net.Documents;
using System.Data;
using OgilvyOne.UKNow.Model;
using OgilvyOne.UKNow.DAL;
namespace OgilvyOne.UKNow.BLL
{
    public class NewsBLL : IGenerateHtml, ICnEnMapping
    {
        public int LanguageID { get; set; }
        public NewsBLL(int langID)
        {
            this.LanguageID = langID;
        }
        //新增新闻
        public ResultInfo Save(NewsModel info)
        {
            IDALNews use = NewsDALFactory.Create(LanguageID);
            return use.Save(info);
        }
        //获取所有新闻
        public IList<NewsModel> selectNews()
        {
            IDALNews use = NewsDALFactory.Create(LanguageID);
            return use.GetAllNewsModels();
        }
        //根据活动newsID 获取该新闻信息
        public NewsModel selNew(int newsID)
        {
            IDALNews use = NewsDALFactory.Create(LanguageID);
            return use.GetNewsModelByID(newsID);
        }
        //按新闻标题或发布状态称搜索新闻记录
        public IList<NewsModel> selNewsByTittle(string newTittle, int? state)
        {
            IDALNews use = NewsDALFactory.Create(LanguageID);
            return use.GetNewsModelByTittle(newTittle, state);
        }
        //删除单个新闻的数据(目前页面过来的id是 string)
        public ResultInfo delNews(int nid)
        {
            IList<int?> list = new List<int?>();
            list.Add(nid);
            IDALNews use = NewsDALFactory.Create(LanguageID);
            return use.Delete(list);
        }
        //修个单个新闻的发布状态
        public void modPubOne(int nid)
        {
            List<int?> list = new List<int?>();
            list.Add(nid);

            IDALNews use = NewsDALFactory.Create(LanguageID);

            NewsModel model = use.GetNewsModelByID(nid); //查看该新闻的当前发布状态
            if (model.State > 0) //该新闻的当前为发布状态
            {
                use.UnPublish(list);
                //设置为该新闻的state为0 不发布 (传递参数 nid 和 int=0)
            }
            else
            {
                use.Publish(list);
                //设置该新闻的state为1 发布     (传递参数 nid 和 int=1)
            }
        }


        #region 获取新闻列表（分页）
        /// <summary>
        /// 获取新闻列表（分页）
        /// </summary>
        /// <param name="currentPagingIndex">当前页</param>
        /// <param name="pageSize">每页显示记录数</param>
        /// <returns></returns>
        public PagingList GetPagingNewsModel(int currentPagingIndex, int pageSize)
        {
            PagingParameter pagingParams=PagingParameterFactory.CreateInstance(true,currentPagingIndex,pageSize,null);
            return NewsDALFactory.Create(LanguageID).GetAllNewsByPaging(pagingParams);
        }
        #endregion


        #region  静态生成html

        public ITemplateType GetTemplateType(IStaticGeneratableModel model)
        {
            return new NewsTemplateType((NewsModel)model);
        }

        /// <summary>
        ///     生成新闻详情页HTML
        /// </summary>
        /// <param name="model"></param>
        public ResultInfo GenerateHtml(IStaticGeneratableModel baseModel, string dir)
        {
            ResultInfo result = new ResultInfo(false);
            string html = GetHtml(baseModel);
            if (string.IsNullOrEmpty(html)) return result;

            try
            {
                string filename = System.Web.HttpContext.Current.Server.MapPath("~" + dir + "/") + ((NewsModel)baseModel).ID.Value + ".html";
                FileUtil.SaveHtmlFile(filename, html);
                result.Result = true;
            }
            catch (Exception ex)
            {
                result.Message = ex.Message;
            }

            return result;
        }

        //获取模版html
        public string GetHtml(IStaticGeneratableModel baseModel)
        {
            string html = null;
            NewsModel model = (NewsModel)baseModel;
            if (model.TemplateID > 0)
            {
                ////生成HTML
                //CmsVoWrapper vo = new CmsVoWrapper();
                //vo.LanguageID = this.LanguageID;
                //vo.Aid = model.ID.Value;
                //vo.IsHtmlFile = true;
                //vo.EventsModel = model;
                //vo.Tid = model.TemplateID.Value;
                //vo.Cid = model.ColumnID;
                ITemplateType newsTemplateType = new NewsTemplateType(model);
                html = newsTemplateType.GetHtml();
            }

            return html;
        }

        /// <summary>
        /// 根据id获取该id的实体
        /// </summary>
        /// <param name="id"></param>
        /// <returns>返回实体</returns>
        public IStaticGeneratableModel GetModelByID(int id)
        {
            return selNew(id);
        }

        /// <summary>
        /// 根据columnID获取该栏目的所有泛型实体
        /// </summary>
        /// <param name="columnID">栏目ID</param>
        /// <returns>返回该栏目的所有泛型实体</returns>
        public IList<IStaticGeneratableModel> GetAllStaticPublishModels(int? columnID)
        {
            return selectNews().Cast<IStaticGeneratableModel>().ToList<IStaticGeneratableModel>();
        }

        /// <summary>
        /// 将新的字段加入全文索引库
        /// </summary>
        /// <returns></returns>
        public IList<Document> CreateLuceneDocuments()
        {
            IList<NewsModel> models = selectNews();
            IList<Document> documents = new List<Document>();
            foreach (NewsModel model in models)
            {
                Document doc = new Document();
                doc.Add(new Field("html", model.Content, Field.Store.YES, Field.Index.ANALYZED));
                doc.Add(new Field("title", model.Title, Field.Store.YES, Field.Index.ANALYZED));
                doc.Add(new Field("url", string.Format("/{0}/news/{1}.html", (this.LanguageID == 2052) ? "cn" : "en", model.ID.Value), Field.Store.YES, Field.Index.NOT_ANALYZED));
                documents.Add(doc);
            }

            return documents;
        }
        #endregion

        public IList<KeyValuePair<int, string>> RetrieveDropDownListItems()
        {
            IList<NewsModel> models = selectNews();
            IList<KeyValuePair<int, string>> list = new List<KeyValuePair<int, string>>();
            foreach (NewsModel model in models)
                list.Add(new KeyValuePair<int, string>(model.ID.Value, model.Title));

            return list;
        }

        public DataTable GetList()
        {
            return NewsDALFactory.Create(LanguageID).GetAllList();

        }

        public ResultInfo SaveSEOInfo(IStaticGeneratableModel model)
        {
            return ((StaticGeneratableDAL)NewsDALFactory.Create(this.LanguageID)).SaveSEOInfo((StaticGeneratableModel)model);
        }

        public ResultInfo GetSEOInfo(int id)
        {
            return null;
        }

        /// <summary>
        /// 取新闻的手机图片
        /// </summary>
        /// <param name="nid"></param>
        /// <returns></returns>
      public  IList<PhoneImage> GetNewsPhone(int? nid)
      {
          return NewsDALFactory.Create(LanguageID).GetNewsPhone(nid);
      }
    }
}
